package com.cndcare.msc.config.reids;

import com.cndcare.msc.config.MyWebSocket;
import com.cndcare.msc.protobuf.MessageVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class RedisReceiver {

    @Autowired
    private ObjectMapper objectMapper;

    public void broadcast(String message) throws Exception {
        log.info("websocket其余节点broadcast: {}", message);
        MessageVo messageVo = objectMapper.readValue(message, MessageVo.class);

        for (MyWebSocket item : MyWebSocket.webSocketSet) {
            //发送消息.
            synchronized (item.session) {
                ObjectMapper objectMapper = new ObjectMapper();
                item.session.getAsyncRemote().sendText(objectMapper.writeValueAsString(messageVo));
            }
        }
    }
}